package org.lg.service.impl;

import org.lg.config.Resource;
import org.lg.config.Service;
import org.lg.dao.TeacherDao;
import org.lg.dto.BaseDto;
import org.lg.dto.MessageEnum;
import org.lg.entity.Teacher;
import org.lg.page.PageInfo;
import org.lg.service.TeacherService;

import java.util.List;
import java.util.Optional;

//业务逻辑层 分担了控制器层 controller 与  数据访问层 dao的业务逻辑部分的判断与计算，例如下单失败是因为库存不足
@Service("teacherService")//这个名字和controller需要的service的名字对应
public class TeacherServiceImpl implements TeacherService {
    @Resource
    TeacherDao teacherDao;
    @Override

    public BaseDto getAllTeachers() throws Exception {
        List<Teacher> Teachers = teacherDao.getAllTeachers();
        Optional<List<Teacher>> op = Optional.ofNullable(Teachers) ;
        MessageEnum messageEnum = op.isPresent()?MessageEnum.GETALLTEACHERSUCCESS:MessageEnum.GETTEACHERBYIDFAIL;//如果成立 赋值 赋？与：之间 否则：之后
        BaseDto dto = new BaseDto(messageEnum,op.get());//如果没有查到数据 前端拿到的时候失败的枚举和null的集合，如果成功前端拿到的是成功的枚举 和  查询到的集合

        return dto;
    }


    /**
     * 分页查询学生
     * @param pageInfo
     * @return
     */

    public BaseDto getTeacherByPage(PageInfo pageInfo) throws Exception {
        List<Teacher> Teachers = teacherDao.getTeacherByPage(pageInfo);
        Optional<List<Teacher>> op = Optional.ofNullable(Teachers) ;
        MessageEnum messageEnum = op.isPresent()?MessageEnum.GETALLTEACHERSUCCESS:MessageEnum.GETTEACHERBYIDFAIL;//如果成立 赋值 赋？与：之间 否则：之后
        BaseDto dto = new BaseDto(messageEnum,op.get());//如果没有查到数据 前端拿到的时候失败的枚举和null的集合，如果成功前端拿到的是成功的枚举 和  查询到的集合
        dto.setObj(pageInfo);
        return dto;
    }

    public BaseDto getTop6Teacher() throws Exception {
        List<Teacher> Teachers = teacherDao.getTop6Teacher();
        Optional<List<Teacher>> op = Optional.ofNullable(Teachers) ;
        MessageEnum messageEnum = op.isPresent()?MessageEnum.GETALLTEACHERSUCCESS:MessageEnum.GETTEACHERBYIDFAIL;//如果成立 赋值 赋？与：之间 否则：之后
        BaseDto dto = new BaseDto(messageEnum,op.get());//如果没有查到数据 前端拿到的时候失败的枚举和null的集合，如果成功前端拿到的是成功的枚举 和  查询到的集合
        return dto;
    }


    @Override
    public BaseDto getTeacherByTno(String tno) throws Exception {
        Teacher Teacher = teacherDao.getTeacherByTno(tno);
        Optional<Teacher> op = Optional.ofNullable(Teacher) ;
        MessageEnum messageEnum = op.isPresent()?MessageEnum.GETALLTEACHERSUCCESS:MessageEnum.GETTEACHERBYIDFAIL;//如果成立 赋值 赋？与：之间 否则：之后
        BaseDto dto = new BaseDto(messageEnum,op.get());
        return dto;
    }

    @Override
    public BaseDto addTeacher(Teacher Teacher) throws Exception {
        Integer result = teacherDao.addTeacher(Teacher);
        MessageEnum messageEnum = result>0?MessageEnum.ADDTEACHERSUCCESS:MessageEnum.GETTEACHERBYIDFAIL;//如果成立 赋值 赋？与：之间 否则：之后
        BaseDto dto = new BaseDto(messageEnum);
        return dto;
    }
}
